﻿using MES_SystemUI.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MES_SystemUI.UI
{
    public partial class TeamsPutin : MES_SystemUI.UI.Putin
    {
        public TeamsPutin()
        {
            InitializeComponent();
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            this.Load += TeamsPutin_Load;
            comboBox2.SelectionChangeCommitted += ComboBox2_SelectionChangeCommitted;
        }

        private void ComboBox2_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex != -1)
            {
                using (var c = new Context.MES_SystemContext())
                {
                    textBox2.Text = c.users.Find(c.teams.Find(comboBox2.SelectedValue).TeamContact).Phone;
                    textBox1.Text = c.users.Find(c.teams.Find(comboBox2.SelectedValue).TeamContact).UserName;
                }
            }
        }

        private void TeamsPutin_Load(object sender, EventArgs e)
        {
            ComboBoxBingData();
            RefreshText();
        }

        private void RefreshText()
        {
            comboBox2.SelectedIndex = -1;
            comboBox3.SelectedIndex = -1;
            comboBox2.Text = string.Empty;
            comboBox3.Text = string.Empty;
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
        }

        private void ComboBoxBingData()
        {
            using (var c = new Context.MES_SystemContext())
            {
                comboBox2.DataSource = c.teams.ToList();
                comboBox2.DisplayMember = "TeamName";
                comboBox2.ValueMember = "TeamId";
                comboBox3.DataSource = c.users.ToList();
                comboBox3.DisplayMember = "UserName";
                comboBox3.ValueMember = "UserId";
            }
        }
        public override void PutinData()
        {
            if (comboBox2.SelectedIndex == -1)
            {
                MessageBox.Show("班组名称不能为空");
                return;
            }
            if (comboBox3.SelectedIndex == -1)
            {
                MessageBox.Show("班组成员不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("负责人不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("手机号不能为空");
                return;
            }
            using (var c = new Context.MES_SystemContext())
            {
                TeamMember mode = new TeamMember()
                {
                    TeamId = int.Parse(comboBox2.SelectedValue.ToString()),
                    MemberId = int.Parse(comboBox3.SelectedValue.ToString()),
                    CreateUserId = SystemDataConfig.LoginUser.UserId,
                };
                c.Entry(mode).State = System.Data.Entity.EntityState.Added;
                c.SaveChanges();
            }
            RefreshText();
            MessageBox.Show("提交成功！");
        }
    }
}
